import { context } from "./context";
import { Action, type ActionType, type StoreType } from "./type";

function storeReducer(state: StoreType, action: ActionType): StoreType {
  console.log("storeReducer", state, action);
  switch (action.type) {
    case Action.SET_HAS_UPDATE:
      return { ...state, hasUpdate: action.payload as boolean };
    default:
      throw new Error(`Unhandled action type: ${action.type}`);
  }
}

export function StoreProvider({ children }: { children: React.ReactNode }) {
  const [state, dispatch] = useReducer(storeReducer, { hasUpdate: false });
  return <context.Provider value={{ state, dispatch }}>{children}</context.Provider>;
}
